package org.za.communication.core.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DataFormat {

	public static byte[] getDateByte(Date date) {
		byte[] ret = new byte[3];
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		String year = String.valueOf(now.get(Calendar.YEAR));
		ret[0] = (byte) Integer.parseInt(year.substring(2));
		ret[1] = (byte) (now.get(Calendar.MONTH) + 1);
		ret[2] = (byte) now.get(Calendar.DATE);
		return ret;
	}

	public static byte[] getTimeByte(Date date) {
		byte[] ret = new byte[3];
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		ret[0] = (byte) now.get(Calendar.HOUR_OF_DAY);
		ret[1] = (byte) now.get(Calendar.MINUTE);
		ret[2] = (byte) now.get(Calendar.SECOND);
		return ret;
	}

	public static Date getDate(byte[] date, byte[] time) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, 2000 + date[0]);
		c.set(Calendar.MONTH, date[1] - 1);
		c.set(Calendar.DATE, date[2]);
		c.set(Calendar.HOUR_OF_DAY, time[0]);
		c.set(Calendar.MINUTE, time[1]);
		c.set(Calendar.SECOND, time[2]);
		return c.getTime();
	}

	public static Date getDate(byte[] date) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, 2000 + date[0]);
		c.set(Calendar.MONTH, date[1] - 1);
		c.set(Calendar.DATE, date[2]);

		return c.getTime();
	}

	public static byte[] formatString(String str, String encode, byte[] seprator) {
		try {
			byte b[] = str.getBytes(encode);
			byte rtnb[] = new byte[b.length + seprator.length];
			System.arraycopy(b, 0, rtnb, 0, b.length);
			System.arraycopy(seprator, 0, rtnb, b.length, seprator.length);
			return rtnb;
		} catch (Exception ee) {
			return seprator;
		}
	}

	public static byte[] formatString(String str, String encode) {
		try {
			byte b[] = str.getBytes(encode);
			return b;
		} catch (Exception ee) {
			return new byte[0];
		}
	}

	public static String parseString(byte[] str, String encode, byte[] seprator) {
		try {
			boolean hasSep = true;
			if (str.length >= seprator.length) {
				for (int i = 0; i < seprator.length; i++) {
					if (seprator[i] == str[str.length - seprator.length + i]) {

					} else {
						hasSep = false;
						break;
					}
				}
				if (hasSep) {
					byte[] b = new byte[str.length - seprator.length];
					System.arraycopy(str, 0, b, 0, str.length - seprator.length);

				} else {
					return new String(str, encode).trim();
				}
			}
			return new String(str, encode).trim();
		} catch (Exception e) {
			return "";
		}
	}

	public static String parseString(byte[] str, String encode) {
		try {
			return new String(str, encode).trim();
		} catch (Exception e) {
			return "";
		}
	}

	public static double getDegreeDouble(byte[] degree) {
		int num = 1;
		byte duB = degree[0];
		byte fenB = degree[1];
		byte[] miaoB = new byte[2];
		miaoB[0] = degree[2];
		miaoB[1] = degree[3];
		int du = byteToInt(duB);
		int fen = byteToInt(fenB);
		int miao = seqToInt(miaoB);
		if (fen >= 128) {
			fen = fen - 128;
			num = -1;
		}
		return (du + (fen * 10000d + miao) / 60d / 10000d) * num;
	}

	public static byte[] getDegreeBytes(double degree) {
		int num = 1;
		if (degree < 0) {
			num = -1;
			degree = degree * -1;
		}
		byte[] d = new byte[4];
		int du = (int) degree;
		int fen = (int) ((degree - du) * 60);
		int miao = (int) Math.round((((degree - du) * 60 - fen) * 10000));
		if (num == -1) {
			fen = fen + 128;
		}
		byte duB = intToByte(du);
		byte fenB = intToByte(fen);
		byte[] miaoB = intToSeq(miao);
		d[0] = duB;
		d[1] = fenB;
		d[2] = miaoB[0];
		d[3] = miaoB[1];
		return d;
	}

	public static byte[] getDegreeBytes(double degree, short deta) {
		int num = 1;
		if (degree < 0) {
			num = -1;
			degree = degree * -1;
		}
		byte[] d = new byte[4];
		int du = (int) degree;
		int fen = (int) ((degree - du) * 60);
		int miao = (int) Math.round((((degree - du) * 60 - fen) * 10000));

		int fens = (du * 60 * 10000 + fen * 10000 + miao);
		fens += deta;
		d[0] = (byte) (fens / 600000);
		int temp = fens % 600000;
		d[1] = (byte) ((temp / 10000) * num);
		temp %= 10000;
		d[2] = (byte) (temp / 256);
		d[3] = (byte) (temp % 256);
		return d;
	}

	public static Date formatToDate(String date, String str) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(str);
			return format.parse(date);
		} catch (Exception ee) {
			return null;
		}
	}

	public static Date formatToDate(String date) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			return format.parse(date);
		} catch (Exception ee) {
			return null;
		}
	}

	public static String DateToString(Date date, String str) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(str);
			return format.format(date);
		} catch (Exception ee) {
			return "";
		}
	}

	public static String DateToString(Date date) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			return format.format(date);
		} catch (Exception ee) {
			return "";
		}
	}

	public static byte[] intToWord(int data) {
		data = data & 0xffffffff;
		byte[] temp = new byte[3];
		for (int i = 0; i < 3; i++) {
			temp[i] = intToByte((int) (data >>> (16 - i * 8)));
		}
		return temp;
	}

	public static int wordToInt(byte[] data) {
		int b1 = byteToInt(data[0]);
		int b2 = byteToInt(data[1]);
		int b3 = byteToInt(data[2]);

		return (b1 << 16) + (b2 << 8) + b3;
	}

	public static byte[] intToSeq(int data) {
		data = data & 0xffff;
		byte[] temp = new byte[2];
		for (int i = 0; i < 2; i++) {
			temp[i] = intToByte(data >>> (8 - i * 8));
		}
		return temp;
	}

	public static int seqToInt(byte[] data) {
		return ((byteToInt(data[0])) << 8) + byteToInt(data[1]);
	}

	public static int byteToInt(byte data) {
		int a = (int) data;
		a = a & 0xff;
		return a;

	}
	
	public static int byteToInt(byte[] data) {
		int a = (int) data[0];
		a = a & 0xff;
		return a;

	}

	public static byte intToByte(int data) {
		data = data & 0xff;
		int temp = data;
		if (temp > 127) {
			data = temp - 256;
		}
		byte b = (byte) data;
		return b;
	}

	public static byte[] accout(double dou) {
		byte[] b = new byte[4];
		int dd = (int) dou;
		b[0] = (byte) dd;
		dou = (dou - dd) * 60;
		int mm = (int) dou;
		b[1] = (byte) mm;
		dou = (dou - mm) * 10000;
		int ssss = (int) dou;
		b[2] = (byte) (ssss / 256);
		b[3] = (byte) (ssss % 256);
		return b;
	}

	public static byte[] longToDWord(long data) {
		data = data & 0xffffffff;
		byte[] temp = new byte[4];
		for (int i = 0; i < 4; i++) {
			temp[i] = intToByte((int) (data >>> (24 - i * 8)));
		}
		return temp;
	}

	public static long dWordToLong(byte[] data) {
		long b1 = byteToInt(data[0]);
		long b2 = byteToInt(data[1]);
		long b3 = byteToInt(data[2]);
		long b4 = byteToInt(data[3]);

		return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
	}

	public static int getNextSign(byte[] b, int start, byte[] sep) {
		int sepL = sep.length;
		int index = 0;
		for (int i = start; i < b.length; i++) {

			if (b[i] == sep[index]) {
				index++;
				if (index == sepL) {
					return i - start + sepL;
				}
			} else {
				index = 0;
			}
		}
		return b.length - start;
	}

	public static int getNextSign(byte[] b, int start) {
		return b.length - start;
	}

	public static String getEncodeData(byte[] b, int from, int len) {
		String rtn = "";
		for (int i = from; i < from + len; i++) {
			String str = "0123456789ABCDEF";

			rtn += (str.charAt((b[i] >> 4) & 0x0f));
			rtn += (str.charAt(b[i] & 0xf));
		}
		return rtn;
	}

	public static String getEncodeData(byte[] b) {
		return getEncodeData(b, 0, b.length);
	}

	public static byte[] getDecodeData(String str) {
		if (str == null) {
			return null;
		}
		if (str.length() == 0) {
			return null;
		}
		if (str.length() % 2 != 0) {
			return null;
		}
		String temp = str.toUpperCase();
		byte[] b = new byte[str.length() / 2];
		for (int i = 0; i < b.length; i++) {
			int ch1 = temp.charAt(i * 2) - 48;
			int ch2 = temp.charAt(i * 2 + 1) - 48;
			if (ch1 > 10) {
				ch1 = ch1 - 7;
			}
			if (ch2 > 10) {
				ch2 = ch2 - 7;
			}
			ch1 = ch1 * 16 + ch2;
			b[i] = (byte) (ch1 & 0xff);
		}
		return b;
	}
}
